<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<%@include file="/web/page/head.jsp" %>
<title>Insert title here</title>
</head>
<body>
<fieldset class="layui-elem-field" style="margin: 20px;padding:15px;">
        <legend>员工维护--修改信息</legend>
    <form class="layui-form" lay-filter="formA" method="post">
        <div class="layui-form-item">
            <label class="layui-form-label">编号</label>
            <div class="layui-input-inline">
                <input type="text" name="code" readonly lay-verify="required" placeholder="请输入编号" autocomplete="off"
                    class="layui-input" >
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">部门</label>
            <div class="layui-input-inline">
                <select name='codeDept'></select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"></label>
            <div class="layui-input-inline">
                <input type="button" class="layui-btn" lay-submit lay-filter="updDept" value="确定" />
                <input type="button" class="layui-btn" onclick="closeThis()" value="取消" />
            </div>
        </div>
        <input type="hidden" name="action" value="updDept" />
    </form>
    </fieldset>

<script type="text/javascript">
var layer = layui.layer;
var $ = layui.jquery;
var laytpl = layui.laytpl;
var element = layui.element;
var form = layui.form
element.render();
var code = '<%=request.getParameter("code")%>';
initA();
function initA(){
	$.ajax({
		url:"<%=app %>/DepartmentServlet",
		data:{action:'list'},
		dataType:'json',
		type:'post',
		success:function(data){
			console.log(data)
			var temp="<option value=''>请选择部门</option>"
			$.each(data,function(){
				temp+="<option value='"+this.code+"'>"+this.name+"</option>"
			})
			$("select[name='codeDept']").html(temp);
			form.render();
			init();
		}
		
	})
}


function init(){
	var code = '<%=request.getParameter("code")%>';
	$.ajax({
		url:"<%=app %>/EmployeeServlet",
		data:{action:'get',code:code},
		dataType:'json',
		type:'post',
		success:function(data){
			form.val("formA",{code:data.code,codeDept:data.codeDept})
		}
	})
}
function closeThis(){
	var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
	parent.layer.close(index); //再执行关闭   
}
form.on("submit(updDept)",function(data){
	$.ajax({
		url:"<%=app %>/EmployeeServlet",
		data:data.field,
		dataType:'text',
		type:'post',
		success:function(data){
			if(data==1){
				layer.msg('修改成功',closeThis);
			}else if(data==0){
				layer.msg('修改失败');
			}else{
				layer.msg("有毒")
			}
		}
	})
})
</script>
</body>
</html>